﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foundation.ObjectHydrator.Interfaces;
using System.Reflection;

namespace Foundation.ObjectHydrator.Generators
{
    public class TypeGenerator:IGenerator
    {
        Type typeOfEntity = null;
        public Object ChildObject { get; private set; }
        public object[] Parameters { get; private set; }

        public TypeGenerator(Type childObjectType, object[] parameters)
        {
            //ChildObject = childObject;

            //typeOfEntity = Type.GetType(childObject.ToString());
            typeOfEntity = childObjectType;
        }

        #region IGenerator Members

        public object Generate()
        {
            Object instance = Activator.CreateInstance(typeOfEntity);

            Type hydratorType = Type.GetType("Hydra.Hydrator`1").MakeGenericType(typeOfEntity);

            Object theHydrator = Activator.CreateInstance(hydratorType);

            MethodInfo methodInfo = hydratorType.GetMethod("GetSingle");
            //MethodInfo methodInfo = hydratorType.GetMethod("Test");

            try
            {
                instance = methodInfo.Invoke(theHydrator, Parameters);
            }
            catch (Exception ex)
            {

                throw ex;
            }

            return instance;
        }

        #endregion
    }
}
